package com.gale.park.modules.pms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gale.park.common.service.RedisService;
import com.gale.park.modules.pms.dto.CardConditionDTO;
import com.gale.park.modules.pms.mapper.PmsCardMapper;
import com.gale.park.modules.pms.model.PmsCard;
import com.gale.park.modules.pms.model.PmsSeat;
import com.gale.park.modules.pms.service.PmsCardService;
import com.gale.park.modules.pms.service.PmsSeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Gale
 * @since 2022-02-16
 */
@Service
public class PmsCardServiceImpl extends ServiceImpl<PmsCardMapper, PmsCard> implements PmsCardService {

    @Autowired
    private PmsCardMapper cardMapper;
    @Autowired
    private PmsSeatService seatService;
    @Value("${redis.key.prefix.card}")
    private static String REDIS_KEY_PREFIX_CARD_ID;

    @Autowired
    private RedisService redisService;

    @Override
    public boolean create(PmsCard card) {
        // 生成编号
        card.setCardId(getNum());
        // 通过车位编号获取车位信息
        PmsSeat seat = seatService.getSeatById(card.getSeatId());

        card.setSeatNum(seat.getSeatNum());

        int insert = cardMapper.insert(card);
        return insert > 0 ? true : false;
    }


    @Override
    public Page<PmsCard> list(CardConditionDTO condition) {

        Page<PmsCard> page = new Page<>(condition.getPageNum(), condition.getPageSize());

        LambdaQueryWrapper<PmsCard> queryWrapper = new LambdaQueryWrapper<>();

        // 如果IC卡编号不为空
        if (StrUtil.isNotBlank(condition.getCardId())) {
            queryWrapper.eq(PmsCard::getCardId, condition.getCardId());
        }

        //如果车位编号不为空
        if (StrUtil.isNotBlank(condition.getSeatNum())) {
            queryWrapper.eq(PmsCard::getSeatNum, condition.getSeatNum());
        }

        // 如果用户名不为空
        if (StrUtil.isNotBlank(condition.getUserName())) {
            queryWrapper.eq(PmsCard::getUserName, condition.getUserName());
        }

        return this.page(page, queryWrapper);
    }


    @Override
    public boolean deleteCardByIds(List<String> ids) {
        UpdateWrapper<PmsCard> updateWrapper = new UpdateWrapper<>();

        updateWrapper.lambda()
                .in(PmsCard::getCardId, ids);
        return this.remove(updateWrapper);
    }

    @Override
    public boolean deleteCardById(String id) {
        UpdateWrapper<PmsCard> updateWrapper = new UpdateWrapper<>();

        updateWrapper.lambda()
                .eq(PmsCard::getCardId, id);
        return this.remove(updateWrapper);
    }

    @Override
    public PmsCard getCarById(String cardId) {

        QueryWrapper<PmsCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(PmsCard::getCardId, cardId);


        return cardMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean update(PmsCard card) {
        UpdateWrapper<PmsCard> updateWrapper = new UpdateWrapper<>();

        // 通过车位编号获取车位信息
        PmsSeat seat = seatService.getSeatById(card.getSeatId());

        card.setSeatNum(seat.getSeatNum());
        updateWrapper.lambda()
                .eq(PmsCard::getCardId, card.getCardId());

        return this.update(card, updateWrapper);
    }


    /**
     * 生成编号：生成规则:8位日期+6位以上自增id；
     *
     * @return
     */
    public String getNum() {
        // 编号
        StringBuilder sb = new StringBuilder();
        // 8位日期
        String yyyyMMdd = new SimpleDateFormat("yyyyMMdd").format(new Date());
        sb.append(yyyyMMdd);

        // 6位以上自增id
        // redis incr 原子性
        // 适合并发的自增方式：
        String key = REDIS_KEY_PREFIX_CARD_ID + yyyyMMdd;
        Long incr = redisService.incr(key, 1);
        // 拿到当前的6位以上自增 如果小于6位，自动补全2
        if (incr.toString().length() <= 6) {
            sb.append(String.format("%06d", redisService.incr(key, 1)));
        } else {
            // 如果是6位 不用补0
            sb.append(incr);
        }
        return sb.toString();
    }
}
